package top.melody.ecommerce.webapps.console.controller.account;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.melody.ecommerce.common.util.JsonMapper;
import top.melody.ecommerce.common.util.StringUtil;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.platform.api.domain.account.Account;
import top.melody.ecommerce.platform.api.domain.system.SecurityUser;
import top.melody.ecommerce.platform.handler.enums.ResultMsg;
import top.melody.ecommerce.platform.handler.enums.ValidStatus;
import top.melody.ecommerce.webapps.console.base.Constant;
import top.melody.ecommerce.webapps.console.controller.common.BaseController;
import top.melody.ecommerce.webapps.console.weblogic.AccountProducerWeblogic;
import top.melody.ecommerce.webapps.console.weblogic.SecurityUserWeblogic;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/producer")
public class AccountProducerController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AccountProducerController.class);
    @Resource
    private AccountProducerWeblogic accountProducerWeblogic;
    @Resource
    private ResourceBundleMessageSource i18nSource;

    @RequestMapping("/list")
    public ModelAndView queryProducerListByPage(@RequestParam(required = false) String userId,
                                               @RequestParam(required = false) String screenName,
                                               @RequestParam(value = "pager.offset", required = false, defaultValue = "0")Integer startRowIndex,
                                               @RequestParam(value = "maxPageItems", required = false, defaultValue = "10") Integer pageSize) {
        String logicViewName = "account/account-producer-list";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        try {
            Account queryEntity = new Account();
            if (!StringUtil.isEmpty(userId))
                queryEntity.setUserId(userId);
            if (!StringUtil.isEmpty(screenName))
                queryEntity.setScreenName(screenName);
            Pagination page = new Pagination(0, startRowIndex / pageSize + 1, pageSize);
            PageRows<Account> pageRows = accountProducerWeblogic.queryProducerListByPage(queryEntity, page);
            paramMap.put("userId", userId);
            paramMap.put("screenName", screenName);
            paramMap.put("validStatus", ValidStatus.VALID);
            paramMap.put("page", pageRows.getPage());
            paramMap.put("rows", pageRows.getRows());
        } catch (Exception e) {
            logicViewName = "error/500";
            logger.error("query security user list by page error", e);
        }
        return new ModelAndView(logicViewName, paramMap);
    }

    @RequestMapping("/operate")
    @ResponseBody
    public String operateAccountUser(Integer accountId) {
        ResultMsg resultMsg = new ResultMsg(i18nSource.getMessage(Constant.ERROR_GLOBAL_EXCEPTION, null, Locale.CHINA));
        try {
            Account account = accountProducerWeblogic.getAccountUser(accountId);
            resultMsg.setMsg(account.getValidStatus().getCode());
            if (account.getValidStatus().equals(ValidStatus.LOCKED))
                account.setValidStatus(ValidStatus.VALID);
            else
                account.setValidStatus(ValidStatus.LOCKED);
            accountProducerWeblogic.modifyAccountUser(account);
            resultMsg.setStatusCode(ResultMsg.CODE_S);
        } catch (Exception e) {
            logger.error("operate security user failed", e);
        }
        return JsonMapper.nonEmptyMapper().toJson(resultMsg);
    }
}
